package com.example.database;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MailDAO implements IMailDAO {

    private static final String DATABASE_CREATE = "CREATE TABLE mail (" +
            "fileId TEXT PRIMARY KEY NOT NULL," +
            "turn INTEGER NOT NULL," +
            "mailfrom TEXT," +
            "body TEXT NOT NULL," +
            "type TEXT" +
            ");";

    private static final String DATABASE_NAME = "mail";
    private static final String DATABASE_TABLE = "mail";
    private static final int DATABASE_VERSION = 1;


    private final Context context;

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public MailDAO(Context ctx) {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    @Override
    public boolean saveMail(String fileId, Integer turn, String from, String body, String type) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_FILE_ID, fileId);
        initialValues.put(KEY_TURN, turn);
        initialValues.put(KEY_FROM, from);
        initialValues.put(KEY_BODY, body);
        initialValues.put(KEY_TYPE, type);
        return db.insert(DATABASE_TABLE, null, initialValues) > 0;
    }

    @Override
    public boolean deleteMail(String fileId) {
        return db.delete(DATABASE_TABLE, KEY_FILE_ID + "=" + fileId, null) > 0;
    }

    @Override
    public Cursor getMail(String fileId) throws SQLException {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[]{
                        KEY_FILE_ID,
                        KEY_TURN,
                        KEY_FROM,
                        KEY_BODY,
                        KEY_TYPE},
                        KEY_FILE_ID + "=" + fileId,
                        null,
                        null,
                        null,
                        null,
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    @Override
    public Cursor loadAllMail() {
        return db.query(DATABASE_TABLE, new String[]{
                KEY_FILE_ID,
                KEY_TURN,
                KEY_FROM,
                KEY_BODY,
                KEY_TYPE},
                null,
                null,
                null,
                null,
                null);
    }

    @Override
    public IMailDAO open() throws SQLException {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    @Override
    public void close() {
        DBHelper.close();
    }

    @Override
    public Cursor loadMail(String selection, String[] args) {
         return db.query(DATABASE_TABLE, new String[]{
        KEY_FILE_ID,
        KEY_TURN,
        KEY_FROM,
        KEY_BODY,
        KEY_TYPE},
        selection,
        args,
        null,
        null,
        null);


/*
        return db.query(DATABASE_TABLE, new String[]{
                KEY_FILE_ID,
                KEY_TURN,
                KEY_FROM,
                KEY_BODY,
                KEY_TYPE},
                null,
                null,
                null,
                null,
                null);
*/
    }


    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion,
                              int newVersion) {
            Log.w("DatabaseHelper", "Upgrading database from version " + oldVersion
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }

}
